#ifndef PROJECT_H
#define PROJECT_H

#include <QFont>
#include <QPixmap>
#include <QAbstractItemModel>

class Project : public QAbstractTableModel
{
public:

    enum Role
    {
        RectRole = Qt::UserRole + 1,
        PixmapRole
    };

    enum Column
    {
        GlyphColumn = 0,
        CharacterRectColumn,
        WidthColumn,
        TextureRectColumn,
    };

    Project();
    ~Project();

    // model methods

    int rowCount( const QModelIndex & parent ) const;
    int columnCount( const QModelIndex & parent ) const;
    QVariant data(const QModelIndex & index, int role = Qt::DisplayRole ) const;
    bool removeRows(int row, int count, const QModelIndex & parent = QModelIndex() );

    void setFont( const QFont & font );
    void setFontIsOultined( bool outlined )
    {
        FontIsOutlined = outlined;
    }

    QFont & font()
    {
        return Font;
    }

    void addCharacter( int unicode );
    void addCustomCharacter( int unicode, const QPixmap & pixmap, int start, int advance, int baseline );

    bool getCustomCharacter( int index, int & unicode, QPixmap & pixmap, int & start, int & advance, int & baseline );
    void updateCustomCharacter( int index, int unicode, const QPixmap & pixmap, int start, int advance, int baseline );

    const QPixmap & texture() const
    {
        return Texture;
    }

    void save( QIODevice & file );
    bool load( QIODevice & file );

    void saveInABCFormat( const QString & abc_file_path, const QString & texture_file_path );

private:

    struct Glyph
    {
        bool IsCustom;
        QChar Code;
        QRect TextureRect; // in pixels
        QRect CharacterRect;
        float   Width;
        QPixmap Pixmap;
    };

    Project( const Project & other );
    Project & operator=( const Project & other );

    void createGlyph( Glyph & glyph, QChar code );
    void updateTexture();

    QFont Font;
    QPixmap Texture;
    
    QVector<Glyph> GlyphTable;

    bool FontIsOutlined;
};

#endif